package com.example.demo.sevice.imp;

import com.example.demo.controller.data.ReturnData;
import com.example.demo.dao.BannerDao;
import com.example.demo.sevice.BannerService;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;

@Service
public class BannerServiceImp implements BannerService {
    @Resource
    BannerDao bannerDao;
    @Override
    public ReturnData getBanner() {
        return ReturnData.ok(bannerDao.findAll());
    }

    @Override
    public ReturnData addBanner(Banner banner) {
        return null;
    }

    @Override
    public ReturnData deleteBanner(Banner banner) {
        if (banner.getId()==1){
            banner.setAddTime(new Timestamp(System.currentTimeMillis()));
            banner.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        }else {
            banner.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        }
        if (StringUtils.isEmpty(banner.getUrl())){
            return ReturnData.error("图片不能为空");
        }
        bannerDao.save(banner);
        return ReturnData.ok(null,"操作成功");
    }

    @Override
    public ReturnData deleteBanner(int id){
        Banner banner = bannerDao.findById(id).get();
        if(banner==null){
            return ReturnData.error("评论不存在");
        }
        bannerDao.delete(banner);
        return ReturnData.ok("删除成功");
    }
}
